package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.AdminStorageService;
import com.cskaoyan.market.db.common.BasePageInfo;
import com.cskaoyan.market.db.common.CommonData;
import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 小焦
 * @since 2023/03/19 00:44
 */
public class AdminStorageServiceImpl implements AdminStorageService {
    // @Override
    // public Integer create(HttpServletRequest req, HttpServletResponse resp) {
    // int code = FileUploadUtils.uploadImg(req, resp);
    // BaseStorageCreateVo createVo = FileUploadUtils.createVo;
    // MarketStorage marketStorage = ReflectUtils.copyObject(createVo, MarketStorage.class);
    // SqlSession sqlSession = MyBatisUtil.getSession();
    //
    // MarketStorageMapper storageMapper = sqlSession.getMapper(MarketStorageMapper.class);
    //
    // int i = storageMapper.insertSelective(marketStorage);
    //
    // sqlSession.commit();
    // sqlSession.close();
    // System.out.println(i);
    // if (i < 0) {
    // return 252;
    // }
    // return code;
    // }

    @Override
    public CommonData<MarketStorage> list(BasePageInfo basePageInfo, String key, String name) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketStorageMapper storageMapper = sqlSession.getMapper(MarketStorageMapper.class);

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketStorageExample storageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = storageExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(key)) {
            criteria.andKeyIsNotNull().andKeyLike("%" + key.trim() + "%");
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameIsNotNull().andNameLike("%" + name.trim() + "%");
        }
        storageExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        List<MarketStorage> marketStorages = storageMapper.selectByExample(storageExample);
        sqlSession.commit();
        sqlSession.close();
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketStorages);
        CommonData<MarketStorage> data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public MarketStorage update(MarketStorage marketStorage) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketStorageMapper storageMapper = sqlSession.getMapper(MarketStorageMapper.class);
        LocalDateTime now = LocalDateTime.now();
        marketStorage.setUpdateTime(now);
        // 先修改 , 在查找
        int row = storageMapper.updateByPrimaryKeySelective(marketStorage);

        // 修改后在查找一下-- 这行代码可以不要
        MarketStorage data = storageMapper.selectByPrimaryKey(marketStorage.getId());
        sqlSession.commit();
        sqlSession.close();
        if (row < 1) {
            return null;
        }
        return data;
    }

    @Override
    public int delete(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketStorageMapper storageMapper = sqlSession.getMapper(MarketStorageMapper.class);
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setId(id);
        marketStorage.setUpdateTime(LocalDateTime.now());
        marketStorage.setDeleted(true);
        storageMapper.updateByPrimaryKeySelective(marketStorage);

        sqlSession.commit();
        sqlSession.close();
        return 0;
    }

    @Override
    public Integer create(MarketStorage storage) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketStorageMapper storageMapper = sqlSession.getMapper(MarketStorageMapper.class);

        int i = storageMapper.insertSelective(storage);

        sqlSession.commit();
        sqlSession.close();
        return i;
    }
}
